package com.zuijiajiao.servlab.databaseWEB.JDBC;

import java.beans.Statement;
import java.sql.*;


public class ConnectionFacroty {

	//oracle.jdbc.driver.OracleDriver
	private static final String Driver="oracle.jdbc.driver.OracleDriver";
	//jdbc:oracle:thin:@localhost:1521:ORCL
	private static final String URL="jdbc:oracle:thin:@localhost:1521:ORCL";
	private static final String USER="sance2016";
	private static final String password="sance2016123";
	
	//获取连接
	public static Connection getConnection() throws ClassNotFoundException {
		Connection connection;
		try{
			Class.forName(Driver);
			connection=DriverManager.getConnection(URL,USER,password);
			return connection;
		}catch(SQLException ex){
			ex.printStackTrace();
			System.out.println("=================数据库连接错误====================");
			return null;
		}
		
	}
	public static void close(Connection connection){
		try{
			if(connection!=null){
				connection.close();
			}
		}catch (SQLException ex) {
			System.out.println("=======数据库连接错误====");
			ex.printStackTrace();
		}
	}
	public static void close(Statement statement,ResultSet resultSet) {
	
		try{
			if(statement!=null)
				 ((Connection) statement).close(); 
			if(resultSet!=null )   
				resultSet.close(); 
		}catch(SQLException ex){
			ex.printStackTrace();
		}
	}
	public static void close(Connection connection,PreparedStatement preparedStatemen, ResultSet resultSet) {
		try{
			if(connection!=null)
				connection.close();
			if(preparedStatemen!=null)
				preparedStatemen.close();
			if(resultSet!=null)
				resultSet.close();
			
		}catch(SQLException ex){
			System.out.println("数据库连接失败");
			ex.printStackTrace();
		}
		
	}
	

}
